public with sharing class Aramex_Smartpaperbpm_CreatePickup{

    public static string url = null;
    public static string soapXML = null;
    //Public Static String WebParam {get; set;}
    public static string stdemo {get; set;}
    public static string xml = null;
    Public static Boolean flag = true;
    
    
    public static void AramexCreatePickup(Map<String, String> sfparameters)
    {    
        //set endpoint
        //url = 'http://eyeegypt.smartappsegypt.com/msd/Aramex/afza/soap_server/AramexIntegrationService2.php';  
        url = 'https://www.afzaeforms.com/soap_server/AramexSoapCreatePickup.php?wsdl';  

    
        //build the Create Pickup XML
        soapXML = '<?xml version="1.0" encoding="ISO-8859-1"?>';
        soapXML += '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:servicename">';
        soapXML += '<SOAP-ENV:Body>';
        soapXML += '<tns:createAramexPickup xmlns="urn:servicename">';
        soapXML += '<sfinfo xsi:type="tns:SFInfo">';
        /* ClientInfo */        
        soapXML += '<accountcountrycode xsi:type="xsd:string">' + sfparameters.get('AccountCountryCode__c') + '</accountcountrycode>';
        soapXML += '<accountentity xsi:type="xsd:string">' + sfparameters.get('AccountEntity__c') + '</accountentity>';
        soapXML += '<accountnumber xsi:type="xsd:string">' + sfparameters.get('AccountNumber__c') + '</accountnumber>';
        soapXML += '<accountpin xsi:type="xsd:string">' + sfparameters.get('AccountPin__c') + '</accountpin>';
        soapXML += '<username xsi:type="xsd:string">' + sfparameters.get('UserName__c') + '</username>';
        soapXML += '<password xsi:type="xsd:string">' + sfparameters.get('Password__c') + '</password>';
        soapXML += '<version xsi:type="xsd:string">' + sfparameters.get('Version__c') + '</version>';
        /* Pickup */ 
        
        //PickupAddress
        soapXML += '<line1 xsi:type="xsd:string">' + sfparameters.get('AFZA_Address_Line1__c') + '</line1>';
        soapXML += '<line2 xsi:type="xsd:string">' + sfparameters.get('AFZA_Address_Line2__c') + '</line2>';
                    
        soapXML += '<city xsi:type="xsd:string">' + sfparameters.get('AFZA_Address_City__c') + '</city>';               
        soapXML += '<stateorprovince xsi:type="xsd:string">' + sfparameters.get('AFZA_Address_State__c') + '</stateorprovince>';               
        //soapXML += '<countrycode xsi:type="xsd:string">AE</countrycode>';               
        soapXML += '<countrycode xsi:type="xsd:string">' + sfparameters.get('AFZA_Address_Country__c') + '</countrycode>';               
                        
        //Pickup Contact
        //soapXML += '<department xsi:type="xsd:string">' + sfparameters.get('AFZA_Department__c') + '</department>';
        soapXML += '<personname xsi:type="xsd:string">' + sfparameters.get('AFZA_Person_Name__c') + '</personname>';
        //soapXML += '<title xsi:type="xsd:string">' + sfparameters.get('AFZA_Person_Title__c') + '</title>';
        soapXML += '<companyname xsi:type="xsd:string">' + sfparameters.get('CompanyName__c') + '</companyname>';
        soapXML += '<cellphone xsi:type="xsd:string">' + sfparameters.get('CellPhone__c') + '</cellphone>';
        soapXML += '<phonenumber xsi:type="xsd:string">' + sfparameters.get('PhoneNumber1__c') + '</phonenumber>';
        soapXML += '<emailaddress xsi:type="xsd:string">' + sfparameters.get('AFZA_Person_Email__c') + '</emailaddress>';
                        
        //Shipment number
        //soapXML += '<shipmentnumber xsi:type="xsd:string">' + sfparameters.get('Shipment_Number__c') + '</shipmentnumber>';
        //servicelettername
        soapXML += '<servicelettername xsi:type="xsd:string">' + sfparameters.get('Name') + '</servicelettername>';
        //pickup date
        //soapXML += '<pickupdate xsi:type="xsd:string">' + sfparameters.get('Courier_Pickup_Date__c') + '</pickupdate>';

        /*Shipment*/
        //shipment consignee location
        soapXML += '<consigneeline1 xsi:type="xsd:string">' + sfparameters.get('Courier_Delivery_Address__c') + '</consigneeline1>';
        soapXML += '<consigneecity xsi:type="xsd:string">' + sfparameters.get('Courier_Delivery_City__c') + '</consigneecity>';
        soapXML += '<consigneecountry xsi:type="xsd:string">' + sfparameters.get('Courier_Aramex_Country_Code__c') + '</consigneecountry>';
        //soapXML += '<consigneecountry xsi:type="xsd:string">AE</consigneecountry>';
        
        //shipment contact
        soapXML += '<consigneepersonname xsi:type="xsd:string">' + sfparameters.get('Full_Name_English__c') + '</consigneepersonname>';
        soapXML += '<consigneecompanyname xsi:type="xsd:string">' + sfparameters.get('Company_Name_English__c') + '</consigneecompanyname>';
        soapXML += '<consigneecellphone xsi:type="xsd:string">' + sfparameters.get('Courier_Delivery_Contact_Mobile__c') + '</consigneecellphone>';
        soapXML += '<consigneephonenumber xsi:type="xsd:string">' + sfparameters.get('Courier_Delivery_Contact_Mobile__c') + '</consigneephonenumber>';
        soapXML += '<consigneeemailaddress xsi:type="xsd:string">' + sfparameters.get('Email__c') + '</consigneeemailaddress>';

        soapXML += '</sfinfo></tns:createAramexPickup>';
        soapXML += '</SOAP-ENV:Body>';
        soapXML += '</SOAP-ENV:Envelope>';

    
        // Instantiate a new http object
        Http h = new Http();
              
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        
        //Initializevariables();
        
        req.setBody(soapXML);
        req.setEndpoint(url);
        req.setMethod('POST');
        req.setHeader('Content-length', '510' );
        req.setHeader('Content-Type', 'text/xml; charset=ISO-8859-1');  
        //req.setHeader('SOAPAction','"http://demo.smartpaperbpm.com/difc/soap_server/hellowsdl1.php#DoSomething"');
        stdemo = '';
        if(flag)
        {    

            if(!Test.isRunningTest())
            {
                HttpResponse res = h.send(req);                
                xml = res.getBody();
                
               
                
                XmlStreamReader reader = res.getXmlStreamReader();                

                
                //stdemo = readXMLResponse(reader,'sfreturn');
                stdemo = getValueFromXMLString(xml, 'sfreturn', 0);
                stdemo = stdemo.unescapeHtml4();
                //stdemo = xml;
                
                System.debug('The XML Response => '+ stdemo);
            }
        }
    }
// This is an extra function to read data between opening and closing tag. 
// It will return the string of value from between tags
/*
public static string getValueFromTag(XMLStreamReader reader) {
    String DataValue;

    while (reader.hasNext()) {
        if (reader.getEventType() == XmlTag.END_ELEMENT) {
            break;
        } else if (reader.getEventType() == XmlTag.CHARACTERS) {
            DataValue = reader.getText();
        }
        reader.next();
    }

    return DataValue;
}
    
    public static String readXMLResponse(XmlStreamReader reader, String sxmltag)
    {
        string retValue;
        // Read through the XML
        system.debug('readxmlme => ' + reader.toString());
        while(reader.hasNext()) {
            if (reader.getEventType() == XmlTag.START_ELEMENT) {
                if (reader.getLocalName() == sxmltag) {
                    reader.next(); 
                    if (reader.getEventType() == XmlTag.characters) {
                        retValue = reader.getText();    
                    }
                } 
            }
            reader.next();
        } 
        return retValue;
    } 
    */
    
public static string getValueFromXMLString(string xmlString, string keyField, integer location){
    string valueFound = '';
    if(xmlString.contains('<' + keyField + ' xsi:type=\"xsd:string\">')) { // && xmlString.contains('<!--' + keyField + '-->')){
      try{
      //system.debug('myval =>' + xmlString.substring(xmlString.indexOf('<' + keyField + ' xsi:type=\"xsd:string\">',0)));
          valueFound = xmlString.substring(xmlString.indexOf('<' + keyField + ' xsi:type="xsd:string">', location) + keyField.length() + 24,
            xmlString.indexOf('</' + keyField + '>', location));   
        }catch (exception e){
            system.debug('Error in getValueFromXMLString.  Details: ' + e.getMessage() + ' keyfield: ' + keyfield);
        }           
    }
    return valueFound;
} 
    
}